package com.banciyuan.bcywebview.biz.main.group.hot;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.main.group.hot.viewholder.ViewGroupBase;
import com.banciyuan.bcywebview.biz.main.group.hot.viewholder.ViewGroupPic;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Team;

/**
 * Created by squall on 2015/8/31.
 */
public class GroupHotAdapterSearch extends BaseAdapter {

    private List<Team> tlist = new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;

    public GroupHotAdapterSearch(Context context, List<Team> tlist) {
        this.mContext = context;
        this.tlist = tlist;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return tlist.size();
    }

    @Override
    public Object getItem(int i) {
        return tlist.get(i);
    }

    @Override
    public int getItemViewType(int position) {
        Team team = tlist.get(position);
        if (team.getType().equals("team")) {
            if (!TextUtils.isEmpty(team.getImg_src())) {
                return 0;
            } else {
                return 2;
            }
        } else if (team.getType().equals("title") || team.getType().equals("circle_title")) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    private class TitleViewHolder {
        private TextView title;

        public TitleViewHolder(View convertView) {
            title = (TextView) convertView.findViewById(R.id.ground_zan_type_tv);
        }
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewGroupPic viewGroupPic = null;
        TitleViewHolder titleViewHolder = null;
        ViewGroupBase viewGroupBase = null;
        if (view == null) {
            switch (getItemViewType(i)) {
                case 0:
                    view = inflater.inflate(R.layout.group_main_item, null);
                    viewGroupPic = new ViewGroupPic(view);
                    view.setTag(viewGroupPic);
                    break;
                case 1:
                    view = inflater.inflate(R.layout.search_result_num, null);
                    titleViewHolder = new TitleViewHolder(view);
                    view.setTag(titleViewHolder);
                    break;
                case 2:
                    view = inflater.inflate(R.layout.group_main_item_without_pic, null);
                    viewGroupBase = new ViewGroupBase(view);
                    view.setTag(viewGroupBase);
                    break;
                default:
                    view = inflater.inflate(R.layout.group_main_item_without_pic, null);
                    viewGroupBase = new ViewGroupBase(view);
                    view.setTag(viewGroupBase);
                    break;
            }
        } else {
            switch (getItemViewType(i)) {
                case 0:
                    viewGroupPic = (ViewGroupPic) view.getTag();
                    break;
                case 1:
                    titleViewHolder = (TitleViewHolder) view.getTag();
                    break;
                case 2:
                    viewGroupBase = (ViewGroupBase) view.getTag();
                    break;
                default:
                    viewGroupBase = (ViewGroupBase) view.getTag();
                    break;
            }
        }
        final Team team = (Team) getItem(i);

        switch (getItemViewType(i)) {
            case 0:
                if (viewGroupPic != null) {
                    viewGroupPic.renderData(mContext, viewGroupPic, team, 2);
                }
                break;
            case 1:
                setCountUI(titleViewHolder, team);
                break;
            case 2:
                if (viewGroupBase != null) {
                    viewGroupBase.renderData(mContext, viewGroupBase, team, 2);
                }
                break;
            default:
                if (viewGroupBase != null) {
                    viewGroupBase.renderData(mContext, viewGroupBase, team, 2);
                }
                break;
        }
        return view;
    }

    private void setCountUI(TitleViewHolder titleViewHolder, Team team) {
        if (!TextUtils.isEmpty(team.getCount())) {
            titleViewHolder.title.setText(Html.fromHtml(team.getCount()));
        }
    }

}
